<?php

session_start();
include_once("../class/db.php");
$db = new db();

if(!array_key_exists("user", $_SESSION) || $_SESSION['account'] != 2){
	header("Location: ../common/redirect.php");
	exit;
}
if(array_key_exists("name", $_POST)){
	$name = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['name']);
	$category = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['category']);
	$description = addslashes($_POST['description']);
	$course_goal = addslashes($_POST['course_goal']);
	$course_type = $_POST['course_type'];
	$after_buy_it = $_POST['after_buy_it'];
	$grade = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['grade']);
	$active = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['active']);
	$prerequisites = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['prerequisites']);
	$prerequisites_validation = preg_replace("/[^a-zA-Z0-9_\-\.\s]/", "", $_POST['prerequisites_validation']);
	$affiliate = $_POST['affiliate'];
	
	$creator = $_SESSION['user'];
	$link = sha1(rand(1,9999999));
	
	if($_POST['start_date'] != "" && $_POST['end_date'] != ""){
		$start_date1 = explode(" ", preg_replace("/[^a-zA-Z0-9_\-:\.\s]/", "", $_POST['start_date']));
		$end_date1 = explode(" ",  preg_replace("/[^a-zA-Z0-9_\-:\.\s]/", "", $_POST['end_date']));
		$start_date2 = explode("-", $start_date1[0]);
		$start_hour = explode(":", $start_date1[1]);
		$end_date2 = explode("-", $end_date1[0]);
		$end_hour = explode(":", $end_date1[1]);

		$start_date = date("Y-m-d H:i:s", mktime($start_hour[0], $start_hour[1], $start_hour[2], $start_date2[1], $start_date2[0], $start_date2[2]));
		$end_date = date("Y-m-d H:i:s", mktime($end_hour[0], $end_hour[1], $end_hour[2], $end_date2[1], $end_date2[0], $end_date2[2]));
		$sql = "INSERT INTO course (name, category, description, course_goal, course_type, after_buy_it, start_date, end_date, grade_format, active, creator, prerequisites, prerequisites_validation, link, affiliate) VALUES ('$name', '$category', '$description', '$course_goal', $course_type, $after_buy_it, '$start_date', '$end_date', $grade, $active, '$creator', '$prerequisites', '$prerequisites_validation', '$link', $affiliate)";
	} else {
		$sql = "INSERT INTO course (name, category, description, course_goal, course_type, after_buy_it, grade_format, active, creator, prerequisites, prerequisites_validation, link, affiliate) VALUES ('$name', '$category', '$description', '$course_goal', $course_type, $after_buy_it, $grade, $active, '$creator', '$prerequisites', '$prerequisites_validation', '$link', $affiliate)";
	}
	$db->execute($sql, "none");
	
	$_SESSION['link'] = $link;
} elseif(array_key_exists("link", $_GET)){
	$_SESSION['link'] = preg_replace("/[^a-zA-Z0-9\s]/", "", $_GET['link']);
}

if(array_key_exists("link", $_SESSION)){
	$sql  = "SELECT * FROM course_modules WHERE link = '".$_SESSION['link']."' ORDER BY order_number ASC";
	$result = $db->execute($sql, "all");
	
	$sql = "SELECT name FROM course WHERE link = '{$_SESSION['link']}'";
	$course_name = $db->execute($sql, "onerow");
} else {
	header("Location: class.php");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PIE EDUCATION | Educational Network</title>
<link rel="stylesheet" type="text/css" href="/css/style-student.css" />
<script type="text/javascript" src="/js/index.js"></script>
<script language="JavaScript" src="/js/ts_picker.js"></script>
<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.7.1.custom.min.js"></script>
<script language="Javascript1.2">
<!--
setTimeout('location.href="/logout.php?expire=1"',600000);
// -->
</script>
<script type="text/javascript">
$(document).ready(function(){ 
						   
	$(function() {
		$("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
			$.post("class-create-content-update.php", order, function(theResponse){
				//$("#contentRight").html(theResponse);
			}); 															 
		}								  
		});
	});

});	
</script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35230372-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="350" height="30">&nbsp;</td>
        <td align="right" valign="bottom"><a href="class.php" class="top-menu" style="color:#999">MY CLASS</a> <a href="schedule.php" class="top-menu" style="color:#999">CALENDAR</a> <a href="homework.php" class="top-menu" style="color:#999">HOMEWORK</a> <a href="test.php" class="top-menu" style="color:#999">EXAMS</a> <a href="grades.php" class="top-menu" style="color:#999">GRADES</a> <a href="tools.php" class="top-menu" style="color:#999">TOOLS</a> <a href="/logout.php" class="top-menu" style="color:#999">SIGN OUT</a></td>
      </tr>
	  <tr>
        <td>&nbsp;</td>
        <td align="right" valign="bottom"><a href="/profile/" class="login-menu" style="color:#FFF"><?php echo $_SESSION['name'].' '.$_SESSION['lname']; ?></a> <a href="/common/redirect.php" class="login-menu" style="color:#FFF">My Documents</a> <a href="/network/" class="login-menu" style="color:#FFF">My Wall</a> <a href="/common/settings.php" class="login-menu" style="color:#FFF">Settings</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style="background-image:url(/images/top-bg-student.png); background-repeat:repeat-x">&nbsp;</td>
  </tr>
  <tr>
  <td height="550" align="center" valign="top"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><img src="../images/course-creation-header-2.png" width="980" height="50" alt="Course Creation" /></td>
      </tr>
      <tr>
        <td height="40" align="right"><input type=button class="submit-course" onClick="location.href='class-content-review.php'" value='Publish My Course' /> <input type=button class="submit-course" onClick="location.href='class-live-preview.php?id=<?php echo $_SESSION['link']; ?>'" value='Live Preview My Course' /></td>
      </tr>
      </table>
    <table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><form id="createclass" name="createclass" method="post" action="class-create-review.php">
          <table width="980" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><h1>Add new module for "<?php echo $course_name['name']; ?>"</h1></td>
              </tr>
            <tr>
              <td style="color:#999"><p style="font-size:14px">To add new content to your course just follow this simple steps. You will be able to add reading material, video course, add an exam and add a glosary</p>
                <ol>
                  <li>Add a new module name below, for example &quot;Introduction to History I&quot;. After this, you will be able to add content to this module. Start date and End date are optional values</li>
                  <li>After finish adding new content to the module, press finish to return to this page and add another module</li>
                  <li>You can do this all the times you want until complete your course</li>
                  </ol></td>
              </tr>
            <tr>
              <td align="center"><table width="350" border="0" cellspacing="4" cellpadding="4">
                <tr>
                  <td><label for="title3">Module Name</label>
                    <input name="title" type="text" class="form-course" id="title" tabindex="1" />
                    <input name="createcontent" type="hidden" id="createcontent" value="module" /></td>
                  </tr>
                </table></td>
              </tr>
            <tr>
              <td height="30" align="center"><input name="submit" type="submit" class="submit-center" id="submit" tabindex="4" value="Create a New Module" /></td>
              </tr>
            </table>
          </form></td>
      </tr>
      <tr>
        <td height="50" align="center"><img src="../images/st.png" width="959" height="11" /></td>
      </tr>
      <?php
	  if($result != false){
	  ?>
      <tr>
        <td><h1>Drag and Drop Modules to Order By</h1></td>
      </tr>
      <tr>
        <td><p style="font-size:14px; color:#999">Below are your modules name and the order on how they are going to be displayed to the student, if you want to reorder the modules, just drag and drop the module box to the place where you want to display it. <strong>To see the content of the module or to add/modify the content, just click on the module name</strong></p></td>
      </tr>
      <tr>
        <td align="center"><div id="contentLeft">
          <ul>
            <?php
				foreach($result as $row){
				?>
            <li id="recordsArray_<?php echo $row['id']; ?>"><a href="class-create-review.php?uid=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></li>
            <?php } ?>
            </ul>
          </div></td>
      </tr>
      <tr>
        <td height="50" align="center">&nbsp;</td>
      </tr>
      <?php
	  }
	  ?>
      </table></td>
      </tr>
  <tr>
    <td height="30" align="center" style=" background-image:url(../images/bottom-bg-student.png); background-repeat:repeat-x;">&nbsp;</td>
  </tr>
  <tr>
    <td height="50"align="center" bgcolor="#000000" style="color:#fff; font-family:Arial, Helvetica, sans-serif">PIE.EDU All Rights Reserved 2012<br />
      <a href="/common/privacy.php" class="bottom-menu" style="color:#999">Privacy Policy</a> | <a href="/common/terms.php" class="bottom-menu"  style="color:#999">Terms of Use</a> | <a href="/help/main.php" class="bottom-menu" style="color:#999">Help Center</a> | <a href="/common/jobs.php" class="bottom-menu" style="color:#999">Jobs</a></td>
  </tr>
    </table>
</body>
</html>